package com.zyao89.view.zloading;

import com.zyao89.view.zloading.util.Log;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.BlendMode;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.lang.ref.WeakReference;

/**
 * Created by zyao89 on 2017/4/9.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class ZLoadingDialog extends ComponentContainer {
    private static final String TAG = "Zloading ZLoadingDialog";
    private final WeakReference<Context> mContext;
    private final int mThemeResId;
    private Z_TYPE mLoadingBuilderType;
    private int mLoadingBuilderColor;
    private String mHintText;
    private int mHintTextSize = -1;
    private int mHintTextColor = -1;
    private boolean mCancelable = true;
    private double mDurationTimePercent = 1.0f;
    private int mDialogBackgroundColor = -1;
    private CommonDialog mZLoadingDialog;

    public ZLoadingDialog(Context context) {
        this(context, 0);
    }

    public ZLoadingDialog(Context context, int themeResId) {
        super(context);
        this.mContext = new WeakReference<>(context);
        this.mThemeResId = themeResId;
    }

    public ZLoadingDialog setLoadingBuilder(Z_TYPE type) {
        this.mLoadingBuilderType = type;
        return this;
    }

    public ZLoadingDialog setLoadingColor(int color) {
        this.mLoadingBuilderColor = color;
        return this;
    }

    public ZLoadingDialog setHintText(String text) {
        this.mHintText = text;
        return this;
    }

    /**
     * 设置了大小后，字就不会有动画了。
     *
     * @param size 大小
     * @return ZLoadingDialog对象
     */
    public ZLoadingDialog setHintTextSize(int size) {
        this.mHintTextSize = size;
        return this;
    }

    public ZLoadingDialog setHintTextColor(int color) {
        this.mHintTextColor = color;
        return this;
    }

    public ZLoadingDialog setCancelable(boolean cancelable) {
        mCancelable = cancelable;
        return this;
    }

    public ZLoadingDialog setDurationTime(double percent) {
        this.mDurationTimePercent = percent;
        return this;
    }

    public ZLoadingDialog setDialogBackgroundColor(int color) {
        this.mDialogBackgroundColor = color;
        return this;
    }

    private Component createContentView() {
        if (isContextNotExist()) {
            throw new RuntimeException("Context is null...");
        }
        Component layoutScatter = LayoutScatter.getInstance(
                mContext.get()).parse(ResourceTable.Layout_z_loading_dialog, this, true);
        return layoutScatter;
    }

    public CommonDialog create() {
        if (isContextNotExist()) {
            throw new RuntimeException("Context is null...");
        }
        if (mZLoadingDialog != null) {
            cancel();
        }
        mZLoadingDialog = new CommonDialog(this.mContext.get());
        mZLoadingDialog.setSize(LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mZLoadingDialog.setAutoClosable(true);
        Component contentView = createContentView();
        DirectionalLayout zLoadingRootView = (DirectionalLayout) contentView.findComponentById(ResourceTable.Id_z_loading);
        ZLoadingView zLoadingView = (ZLoadingView) zLoadingRootView.findComponentById(ResourceTable.Id_z_loading_view);
        if (this.mDialogBackgroundColor != -1) {
            final ShapeElement drawable = new ShapeElement();
            Log.sysI(TAG, "setRgbColor");
            drawable.setRgbColor(RgbColor.fromArgbInt(Color.alpha(ResourceTable.Color_gray)));
            drawable.setStateColorMode(BlendMode.SRC_ATOP);
            drawable.setAlpha(150);
            zLoadingView.setAlpha(255);
            zLoadingView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
            zLoadingRootView.setBackground(drawable);
            mZLoadingDialog.setTransparent(true);
        }
        ZLoadingTextView zTextView = (ZLoadingTextView) contentView.findComponentById(ResourceTable.Id_z_text_view);
        Text zCustomTextView = (Text) contentView.findComponentById(ResourceTable.Id_z_custom_text_view);
        int width = zCustomTextView.getWidth();
        if (this.mHintTextSize > 0 && !TextTool.isNullOrEmpty(mHintText)) {
            zCustomTextView.setVisibility(Component.VISIBLE);
            zCustomTextView.setText(mHintText);
            zCustomTextView.setTextSize(this.mHintTextSize);
            zCustomTextView.setTextColor(this.mHintTextColor == -1 ? new Color(this.mLoadingBuilderColor) : new Color(this.mHintTextColor));
        } else if (!TextTool.isNullOrEmpty(mHintText)) {
            zTextView.setVisibility(Component.VISIBLE);
            zTextView.setText(mHintText);
            zTextView.setImageAndDecodeBounds(this.mHintTextColor == -1 ? this.mLoadingBuilderColor : this.mHintTextColor);
        }
        zLoadingView.setLoadingBuilder(this.mLoadingBuilderType, width);
        // 设置间隔百分比
        if (zLoadingView.mZLoadingBuilder != null) {
            zLoadingView.mZLoadingBuilder.setDurationTimePercent(this.mDurationTimePercent);
        }

        zLoadingView.setImageAndDecodeBounds(this.mLoadingBuilderColor);
        final int type = mLoadingBuilderType.ordinal();
        zLoadingView.setLoadingBuilder(Z_TYPE.values()[type], width);
        zLoadingView.setBindStateChangedListener(zLoadingView);
        mZLoadingDialog.setContentCustomComponent(contentView);
        mZLoadingDialog.setSwipeToDismiss(this.mCancelable);
        return mZLoadingDialog;
    }

    public void show() {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.show();
        } else {
            final BaseDialog zLoadingDialog = create();
            zLoadingDialog.show();
        }
    }

    public void cancel() {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.hide();
        }
        mZLoadingDialog = null;
    }

    public void dismiss() {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.hide();
        }
        mZLoadingDialog = null;
    }

    private boolean isContextNotExist() {
        Context context = this.mContext.get();
        return context == null;
    }
}
